#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "head.h"
#include "cJSON.h"

void insertJsonList(void)
{
    //将数据序列化后存放到文件中
    cJSON *item = NULL;
    //创建一个json目标
    item = cJSON_CreateObject();
    //在这个目标中添加子目标var值为"1.0"
    cJSON_AddItemToObject(item, "var", cJSON_CreateString("1.0"));

    //在这个目标中添加数组子目标cloud得到一个新目标并在其中添加每个元素的成员
    //分别是：电池电压 温湿度 大气压 光照
    cJSON *item_arry = cJSON_AddArrayToObject(item, "cloud");
    //添加数组元素对象并给每个成员子目标赋值
    cJSON *item_data = cJSON_CreateObject();
    cJSON_AddItemToArray(item_arry, item_data);
    cJSON_AddStringToObject(item_data, "name", "battery"); //电池电压
    cJSON_AddNumberToObject(item_data, "up", 20.0);
    cJSON_AddNumberToObject(item_data, "down", 3.0);

    item_data = cJSON_CreateObject();
    cJSON_AddItemToArray(item_arry, item_data);
    cJSON_AddStringToObject(item_data, "name", "humidity"); //温湿度
    cJSON_AddNumberToObject(item_data, "up", 89.9);
    cJSON_AddNumberToObject(item_data, "down", 20.0);

    item_data = cJSON_CreateObject();
    cJSON_AddItemToArray(item_arry, item_data);
    cJSON_AddStringToObject(item_data, "name", "pressure"); //大气压
    cJSON_AddNumberToObject(item_data, "up", 60.0);
    cJSON_AddNumberToObject(item_data, "down", 20.0);

    item_data = cJSON_CreateObject();
    cJSON_AddItemToArray(item_arry, item_data);
    cJSON_AddStringToObject(item_data, "name", "beam"); //光照
    cJSON_AddNumberToObject(item_data, "up", 60.0);
    cJSON_AddNumberToObject(item_data, "down", 20.0);

    char *root = cJSON_Print(item);
    //  printf("%s\n", root);

    int fd = open("./cjson.txt", O_CREAT | O_TRUNC | O_RDWR, 0666);
    if (fd < 0)
    {
        perror("open err.");
        return;
    }

    write(fd, root, strlen(root));

    close(fd);

    cJSON_Delete(item);
}

//获取文件中最高或最低的数据
double getDownOrHigh(const char *name,int op)//0:最低  1：最高
{
    char buf[2000] = "";
    int fd = open("./cjson.txt", O_RDONLY);
    if (fd < 0)
    {
        perror("open err.");
        return -1;
    }

    if (read(fd, buf, 2000) < 0)
    {
        perror("read json file err.");
        return -1;
    }

    cJSON *root = NULL;
    cJSON *arry = NULL;
    cJSON *arry_t = NULL;
    cJSON *node_temp = NULL;
    root = cJSON_Parse(buf);

    //从cjson中取出数组cloud
    arry = cJSON_GetObjectItem(root, "cloud");
    if (arry == NULL)
    {
        printf("get array object err:%s\n", cJSON_GetErrorPtr());
        return -1;
    }
    int arry_size = cJSON_GetArraySize(arry);

    for (int i = 0; i < arry_size; i++)
    {
        arry_t = cJSON_GetArrayItem(arry, i);
        node_temp = cJSON_GetObjectItem(arry_t, "name");
        if ( strcmp(name, node_temp->valuestring)==0 && op==1 )
        {
            node_temp = cJSON_GetObjectItem(arry_t, "up");
            if (node_temp->type == cJSON_Number)
                return node_temp->valuedouble;
        }else if(strcmp(name, node_temp->valuestring)==0 && op==0)
        {
            node_temp = cJSON_GetObjectItem(arry_t, "down");
            if (node_temp->type == cJSON_Number)
                return node_temp->valuedouble;
        }
        
    }
    return -1;
}
